package com.papers.juan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.papers.common.core.domain.PageQuery;
import com.papers.common.core.page.TableDataInfo;
import com.papers.common.utils.StringUtils;
import com.papers.juan.domain.TJuanPaperQuestion;
import com.papers.juan.domain.bo.TJuanPaperQuestionBo;
import com.papers.juan.domain.vo.TJuanPaperQuestionVo;
import com.papers.juan.mapper.TJuanPaperQuestionMapper;
import com.papers.juan.service.ITJuanPaperQuestionService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 试卷题目Service业务层处理
 *
 * @author papers
 * @date 2022-10-08
 */
@RequiredArgsConstructor
@Service
public class TJuanPaperQuestionServiceImpl implements ITJuanPaperQuestionService {

    private final TJuanPaperQuestionMapper baseMapper;

    /**
     * 查询试卷题目
     */
    @Override
    public TJuanPaperQuestionVo queryById(String paperId) {
        return baseMapper.selectVoById(paperId);
    }

    /**
     * 查询试卷题目列表
     */
    @Override
    public TableDataInfo<TJuanPaperQuestionVo> queryPageList(TJuanPaperQuestionBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TJuanPaperQuestion> lqw = buildQueryWrapper(bo);
        Page<TJuanPaperQuestionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询试卷题目列表
     */
    @Override
    public List<TJuanPaperQuestionVo> queryList(TJuanPaperQuestionBo bo) {
        LambdaQueryWrapper<TJuanPaperQuestion> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TJuanPaperQuestion> buildQueryWrapper(TJuanPaperQuestionBo bo) {
//        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TJuanPaperQuestion> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getPaperId()), TJuanPaperQuestion::getPaperId, bo.getPaperId());
//        lqw.eq(StringUtils.isNotBlank(bo.getQuestionId()), TJuanPaperQuestion::getQuestionId, bo.getQuestionId());
        lqw.eq(StringUtils.isNotBlank(bo.getCreatedBy()), TJuanPaperQuestion::getCreatedBy, bo.getCreatedBy());
        lqw.eq(bo.getCreatedTime() != null, TJuanPaperQuestion::getCreatedTime, bo.getCreatedTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdatedBy()), TJuanPaperQuestion::getUpdatedBy, bo.getUpdatedBy());
        lqw.eq(bo.getUpdatedTime() != null, TJuanPaperQuestion::getUpdatedTime, bo.getUpdatedTime());
        return lqw;
    }

    /**
     * 新增试卷题目
     */
    @Override
    public Boolean insertByBo(TJuanPaperQuestionBo bo) {
        TJuanPaperQuestion add = BeanUtil.toBean(bo, TJuanPaperQuestion.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setPaperId(add.getPaperId());
        }
        return flag;
    }

    /**
     * 修改试卷题目
     */
    @Override
    public Boolean updateByBo(TJuanPaperQuestionBo bo) {
        TJuanPaperQuestion update = BeanUtil.toBean(bo, TJuanPaperQuestion.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TJuanPaperQuestion entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除试卷题目
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
